package org.example.controller;

import org.example.bean.R;
import org.example.entity.Adminlogin;
import org.example.mapper.AdminLoginMapper;
import org.example.service.AdminLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class AdminloginController {
    
    @Autowired
    private AdminLoginService adminLoginService;
    
    @Autowired
    private AdminLoginMapper adminLoginMapper;
    
    @PostMapping("/adminlogin")
    public R getAdminLogin(@RequestBody Map<String, String> body) {
        try {
            String account = body.get("account");
            String password = body.get("password");
            
            // 参数验证
            if (account == null || account.trim().isEmpty()) {
                return new R().setCode(400).setMsg("账号不能为空");
            }
            if (password == null || password.trim().isEmpty()) {
                return new R().setCode(400).setMsg("密码不能为空");
            }
            
            // 使用新的登录验证方法
            Adminlogin admin = adminLoginService.login(account, password);
            
            if (admin != null) {
                return new R().setMsg("登录成功")
                        .add("admin", admin)
                        .add("adminId", admin.getId())
                        .add("account", admin.getAccount());
            } else {
                return new R().setCode(401).setMsg("账号或密码错误");
            }
        } catch (Exception e) {
            return new R().setCode(500).setMsg("登录失败，服务器内部错误");
        }
    }
    
    /**
     * 测试接口：检查管理员登录表
     */
    @GetMapping("/admin/test")
    public R testAdminLogin() {
        try {
            return new R().setMsg("管理员登录接口正常").add("message", "请使用POST /adminlogin进行登录");
        } catch (Exception e) {
            return new R().setCode(500).setMsg("测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 调试接口：查看数据库中的所有管理员数据
     */
    @GetMapping("/admin/debug/data")
    public R debugAdminData() {
        try {
            // 查询所有管理员数据
            List<Adminlogin> allAdmins = adminLoginMapper.selectList(null);
            
            return new R().setMsg("数据库查询成功")
                    .add("totalCount", allAdmins.size())
                    .add("admins", allAdmins);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 调试接口：测试特定账号的查询
     */
    @GetMapping("/admin/debug/query")
    public R debugQueryAdmin(@RequestParam String account) {
        try {
            Adminlogin admin = adminLoginMapper.getAdminByAccount(account);
            
            if (admin != null) {
                return new R().setMsg("查询成功").add("admin", admin);
            } else {
                return new R().setCode(404).setMsg("未找到该管理员");
            }
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 调试接口：测试密码验证
     */
    @GetMapping("/admin/debug/password")
    public R debugPassword(@RequestParam String password) {
        try {
            // 测试admin账号的密码
            String adminEncrypted = "$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi";
            boolean adminMatch = org.example.util.PasswordEncoder.matches(password, adminEncrypted);
            
            // 测试superadmin账号的密码
            String superadminEncrypted = "$2a$10$8K1p/a0dL1LXMIgoEDFrwOfgqwAG6a3TqJhBA8B6Eo8UR3qHhVKGi";
            boolean superadminMatch = org.example.util.PasswordEncoder.matches(password, superadminEncrypted);
            
            return new R().setMsg("密码验证测试完成")
                    .add("testPassword", password)
                    .add("adminMatch", adminMatch)
                    .add("superadminMatch", superadminMatch);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("测试失败: " + e.getMessage());
        }
    }
}
